package com.amap.flutter.map.overlays.marker;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

class TriangleDrawable extends Drawable {
    private final Paint fillPaint;
    private final Paint strokePaint;

    TriangleDrawable(int fillColor, int strokeColor, float strokeWidthPx) {
        fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        fillPaint.setStyle(Paint.Style.FILL);
        fillPaint.setColor(fillColor);

        strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setColor(strokeColor);
        strokePaint.setStrokeWidth(strokeWidthPx);
    }

    @Override
    public void draw(Canvas canvas) {
        Rect b = getBounds();
        float midX = b.exactCenterX();
        Path path = new Path();
        path.moveTo(b.left, b.top);
        path.lineTo(b.right, b.top);
        path.lineTo(midX, b.bottom);
        path.close();
        canvas.drawPath(path, fillPaint);
        if (strokePaint.getStrokeWidth() > 0) {
            canvas.drawPath(path, strokePaint);
        }
    }

    @Override
    public void setAlpha(int alpha) {
        fillPaint.setAlpha(alpha);
        strokePaint.setAlpha(alpha);
        invalidateSelf();
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        fillPaint.setColorFilter(colorFilter);
        strokePaint.setColorFilter(colorFilter);
        invalidateSelf();
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}


